/*
 * JGO2RDF creates a RDF representation of GAF2 files, which are automatically sourced from GO ftp site.
 * Copyright (C) 2012 Matthew M. Hindle & Artem Lysenko
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package uk.co.jgo2rdf.tools;

import com.ximpleware.NavException;
import com.ximpleware.VTDNav;

/**
 * Created by IntelliJ IDEA.
 * User: matth
 * Date: 21/10/11
 * Time: 10:10
 * To change this template use File | Settings | File Templates.
 */
public class VTDFactoryTools {

    /**
     * Gets the text within the first element of the given name
     *
     * @param vn          the current document navigator
     * @param elementName the name of the element to find
     * @return the text in the element
     * @throws com.ximpleware.NavException if we can't find the element
     */
    public static String getTextFromFirstElement(VTDNav vn, String elementName) throws NavException {
        if (vn.toElement(VTDNav.FIRST_CHILD, elementName)) {
            try {
                int j = vn.getText();
                if (j != -1) return vn.toString(j);
            } finally {
                vn.toElement(VTDNav.PARENT); // move the cursor back
            }
        }
        return null;
    }

    /**
     * Gets the text within the first element of the given name
     *
     * @param vn          the current document navigator
     * @param elementName the name of the element to find
     * @return the text in the element
     * @throws com.ximpleware.NavException if we can't find the element
     */
    public static int getIntegerFromFirstElement(VTDNav vn, String elementName) throws NavException {
        if (vn.toElement(VTDNav.FIRST_CHILD, elementName)) {
            try {
                int j = vn.getText();
                if (j != -1) return vn.parseInt(j);
            } finally {
                vn.toElement(VTDNav.PARENT); // move the cursor back
            }
        }
        return 0;
    }

    /**
     * Gets the text within the first element of the given name
     *
     * @param vn          the current document navigator
     * @param elementName the name of the element to find
     * @return the text in the element
     * @throws NavException if we can't find the element
     */
    public static double getDoubleFromFirstElement(VTDNav vn, String elementName) throws NavException {
        if (vn.toElement(VTDNav.FIRST_CHILD, elementName)) {
            try {
                int j = vn.getText();
                if (j != -1) return vn.parseDouble(j);
            } finally {
                vn.toElement(VTDNav.PARENT); // move the cursor back
            }
        }
        return 0d;
    }
}
